package org.example.library.controller;

import com.alibaba.fastjson.JSONObject;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.example.library.HelloServlet;
import org.example.library.service.BookService;
import org.example.library.util.JsonUtils;
import org.example.library.util.ResponseUtil;
import org.example.library.vertile.Book;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

@WebServlet("/book/*")
public class BookController extends HttpServlet {
    BookService bookService = new BookService();
    public boolean isLogin(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        if(req.getSession().getAttribute("user") == null){
            ResponseUtil.genBaseResponse(resp, 404, "请先登录", null);
            return true;
        }else{
            return false;
        }

    }

    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=UTF-8");
        resp.setCharacterEncoding("utf-8");
        if(isLogin(req,resp)){
           return;
        }
        String uri = req.getRequestURI();

        switch (uri){
            case "/book/insert"->doInsert(req,resp);
            case "/book/del"->doDelete(req,resp);
            case "/book/update"->doUpdate(req,resp);
            default->{
            }
        }
    }
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=UTF-8");
        resp.setCharacterEncoding("utf-8");
        if(isLogin(req,resp)){
            return;
        }
        String uri = req.getRequestURI();
        if("/book/search".equals(uri)){
            doSearch(req,resp);
        }
    }

    private void doUpdate(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String number = req.getParameter("author");

        if (number == null || number.trim().isEmpty()) {
            ResponseUtil.gen404Response(resp, "你所发送的数据不全");
        }
        Book book = new Book();
        book = bookService.updateBook(number);

        if(book==null){
            ResponseUtil.gen500Response(resp,"找不到要找的书籍");
        }else{
            ResponseUtil.genSuccessResponse(resp,"查找成功","null");
        }
    }

    protected void doDelete(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String number = req.getParameter("number");
        if(number==null||number.trim().isEmpty()){
            ResponseUtil.genBaseResponse(resp,500,"不能为空",null);
        }
        int res = bookService.deleteBook(number);
        switch(res){
            case 0 -> ResponseUtil.gen404Response(resp,"删除失败");
            case 1 -> ResponseUtil.genSuccessResponse(resp,"删除成功","null");
        }
    }


    private void doSearch(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String number = req.getParameter("number");
        if(number==null||number.trim().isEmpty()){
            ResponseUtil.genBaseResponse(resp,500,"不能为空",null);
        }
        Book book = bookService.searchBook(number);
        if(book==null){
            ResponseUtil.gen404Response(resp,"书籍不存在");
        }else{
            ResponseUtil.genSuccessResponse(resp,"查找成功","null");
        }
    }



    private void doInsert(HttpServletRequest req, HttpServletResponse resp) throws IOException {
//        System.out.println("13254");
        if (new BookService().getBookFromCsv()) {
            ResponseUtil.gen500Response(resp, "插入完成，部分书籍已存在");
        } else {
            ResponseUtil.genSuccessResponse(resp, "插入成功", null);
        }
    }

}
